﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
using System.Xml.XPath;

namespace FTI.SP.LADM.EnhancedContentQueryWebPart.Exslt
{
    public sealed class ExsltRegularExpressions
    {
        // Fields
        private static ExsltRegularExpressions _Instance;

        // Methods
        private ExsltRegularExpressions()
        {
        }

        public static XPathNodeIterator Match(string str, string regExp)
        {
            RegexOptions eCMAScript = RegexOptions.ECMAScript;
            XmlDocument document = new XmlDocument();
            document.LoadXml("<matches/>");
            Regex regex = new Regex(regExp, eCMAScript);
            foreach (Group group in regex.Match(str).Groups)
            {
                XmlElement newChild = document.CreateElement("match");
                newChild.InnerText = group.Value;
                document.DocumentElement.AppendChild(newChild);
            }
            return document.CreateNavigator().Select("//match");
        }

        public static XPathNodeIterator Match(string str, string regExp, string parameters)
        {
            RegexOptions eCMAScript = RegexOptions.ECMAScript;
            if (parameters.IndexOf("m", StringComparison.CurrentCulture) != -1)
            {
                eCMAScript |= RegexOptions.Multiline;
            }
            if (parameters.IndexOf("i", StringComparison.CurrentCulture) != -1)
            {
                eCMAScript |= RegexOptions.IgnoreCase;
            }
            XmlDocument document = new XmlDocument();
            document.LoadXml("<matches/>");
            Regex regex = new Regex(regExp, eCMAScript);
            if (parameters.IndexOf("g", StringComparison.CurrentCulture) != -1)
            {
                foreach (Match match in regex.Matches(str))
                {
                    XmlElement newChild = document.CreateElement("match");
                    newChild.InnerText = match.Value;
                    document.DocumentElement.AppendChild(newChild);
                }
            }
            else
            {
                foreach (Group group in regex.Match(str).Groups)
                {
                    XmlElement element2 = document.CreateElement("match");
                    element2.InnerText = group.Value;
                    document.DocumentElement.AppendChild(element2);
                }
            }
            return document.CreateNavigator().Select("//match");
        }

        public static string Replace(string input, string regExp, string parameters, string replacement)
        {
            RegexOptions eCMAScript = RegexOptions.ECMAScript;
            if (parameters.IndexOf("i", StringComparison.CurrentCulture) != -1)
            {
                eCMAScript |= RegexOptions.IgnoreCase;
            }
            Regex regex = new Regex(regExp, eCMAScript);
            if (parameters.IndexOf("g", StringComparison.CurrentCulture) != -1)
            {
                return regex.Replace(input, replacement, 1);
            }
            return regex.Replace(input, replacement);
        }

        public static bool Test(string str, string regExp)
        {
            RegexOptions eCMAScript = RegexOptions.ECMAScript;
            Regex regex = new Regex(regExp, eCMAScript);
            return regex.IsMatch(str);
        }

        public static bool Test(string str, string regExp, string parameters)
        {
            RegexOptions eCMAScript = RegexOptions.ECMAScript;
            if (parameters.IndexOf("m", StringComparison.CurrentCulture) != -1)
            {
                eCMAScript |= RegexOptions.Multiline;
            }
            if (parameters.IndexOf("i", StringComparison.CurrentCulture) != -1)
            {
                eCMAScript |= RegexOptions.IgnoreCase;
            }
            Regex regex = new Regex(regExp, eCMAScript);
            return regex.IsMatch(str);
        }

        public static XPathNodeIterator Tokenize(string str, string regExp)
        {
            RegexOptions eCMAScript = RegexOptions.ECMAScript;
            XmlDocument document = new XmlDocument();
            document.LoadXml("<matches/>");
            Regex regex = new Regex(regExp, eCMAScript);
            foreach (string str2 in regex.Split(str))
            {
                XmlElement newChild = document.CreateElement("match");
                newChild.InnerText = str2;
                document.DocumentElement.AppendChild(newChild);
            }
            return document.CreateNavigator().Select("//match");
        }

        public static XPathNodeIterator Tokenize(string str, string regExp, string parameters)
        {
            RegexOptions eCMAScript = RegexOptions.ECMAScript;
            if (parameters.IndexOf("m", StringComparison.CurrentCulture) != -1)
            {
                eCMAScript |= RegexOptions.Multiline;
            }
            if (parameters.IndexOf("i", StringComparison.CurrentCulture) != -1)
            {
                eCMAScript |= RegexOptions.IgnoreCase;
            }
            XmlDocument document = new XmlDocument();
            document.LoadXml("<matches/>");
            Regex regex = new Regex(regExp, eCMAScript);
            foreach (string str2 in regex.Split(str))
            {
                XmlElement newChild = document.CreateElement("match");
                newChild.InnerText = str2;
                document.DocumentElement.AppendChild(newChild);
            }
            return document.CreateNavigator().Select("//match");
        }

        // Properties
        public static ExsltRegularExpressions Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new ExsltRegularExpressions();
                }
                return _Instance;
            }
        }

    }
}
